package com.example.maze;

import receivers.CreatRoomRepReceiver;
import receivers.StartGameRepReceiver;
import receivers.UpdateRoomStatusRepReceiver;
import services.CreatRoomService;
import services.LeaveRoomService;
import services.StartGameService;
import services.UpdateRoomStatusService;
import services.UpdateRoomsListService;
import tw.edu.tku.im.maze.DeliveryListenerI;
import tw.edu.tku.im.maze.LeaveRoomDeliverer;
import tw.edu.tku.im.maze.LeaveRoomRep;
import tw.edu.tku.im.maze.LeaveRoomReq;
import tw.edu.tku.im.maze.MazeMap;
import tw.edu.tku.im.maze.Member;
import tw.edu.tku.im.maze.RepPacket;
import tw.edu.tku.im.maze.TimeOutRep;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class RoomActivity extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_room);
      
      //[ 註冊廣播
      UpdateRoomStatusRepReceiver mUpdateRoomStatusRepReceiver= new UpdateRoomStatusRepReceiver(this);
      LocalBroadcastManager.getInstance(this).registerReceiver(mUpdateRoomStatusRepReceiver,
                                                               new IntentFilter(UpdateRoomStatusService.BROADCAST_ACTION));
    //[ 註冊廣播
      StartGameRepReceiver mStartGameRepReceiver= new StartGameRepReceiver(this);
      LocalBroadcastManager.getInstance(this).registerReceiver(mStartGameRepReceiver,
                                                               new IntentFilter(StartGameService.BROADCAST_ACTION));
      
      if(ConnectionInfo.identity == ConnectionInfo.HE_IS_HOST) {
         TextView tv;
         tv= (TextView)this.findViewById(R.id.TextView_roomName);
         if(ConnectionInfo.roomName != null) tv.setText(ConnectionInfo.roomName);      
         tv= (TextView)this.findViewById(R.id.TextView_roomOwner);
         if(ConnectionInfo.name != null) tv.setText(ConnectionInfo.name);
         tv= (TextView)this.findViewById(R.id.TextView_mapLv);
         if(ConnectionInfo.mapLv>0) tv.setText(ConnectionInfo.mapLv+"");
      } else { // ,else He is client
         this.findViewById(R.id.button_startGame).setEnabled(false);
      }      
   }
   // ------------------------------------------------------------
   @Override
   protected void onPause() {
      super.onStop();
      ConnectionInfo.roomStatusupdateLoop= ConnectionInfo.INTERRUPT;
   }
   // ------------------------------------------------------------
   @Override
   protected void onResume() {   
      super.onResume();    
      //[ 啟動更新房間列表
      ConnectionInfo.roomStatusupdateLoop= ConnectionInfo.CONTINUE;
      //[ 確保 Service只有一個
      if(ConnectionInfo.theUpdateService == null) {
         //[ 啟動新的 Service         
         this.startService(new Intent(this, UpdateRoomStatusService.class));         
      }      
   }
   // --------------------------------------------------
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.activity_room, menu);
      return true;
   }

   // --------------------------------------------------
   public void button_startGameOnClick(View view) {      
      if(ConnectionInfo.identity == ConnectionInfo.HE_IS_HOST) { 
         this.showMyDialog("", "連線中, 請稍候");
         this.startService(new Intent(this, StartGameService.class));
      }      
   }
   
   // --------------------------------------------------
   public void button_leaveRoomOnClick(View view) {
      if(ConnectionInfo.identity == ConnectionInfo.HE_IS_HOST) {
         ConnectionInfo.identity= ConnectionInfo.HE_IS_CLIENT;
      }
      this.startService(new Intent(this, LeaveRoomService.class));
      Intent intent= new Intent(this, LobbyActivity.class);
      this.startActivity(intent);
   }

   // --------------------------------------------------
   //[ disable back button
   @Override public void onBackPressed() {}
   
   // --------------------------------------------------
   public void update(Member[] players, 
                      boolean  doGameStart, 
                      String   roomName, 
                      int      mapLv, 
                      MazeMap mapInfo) {
android.util.Log.d("DebugMsg","roomStatus updating");      
      ConnectionInfo.roomName= roomName;
      ConnectionInfo.mapLv= mapLv;      
      
      android.util.Log.d("DebugMsg", "=================");
      for(int i=0; i<players.length; i++) {
         android.util.Log.d("DebugMsg", players[i].getName());    
      }
            
      //[ 室長
      TextView tv;
      tv= (TextView)this.findViewById(R.id.TextView_roomName);
      tv.setText(roomName);
      tv= (TextView)this.findViewById(R.id.TextView_roomOwner);
      tv.setText(players[0].getName());
      tv= (TextView)this.findViewById(R.id.TextView_mapLv);
      tv.setText(mapLv+"");      
      //[ 玩家
      String[] mItems= new String[players.length-1];
      for(int i=0; i<players.length-1; i++) {
         mItems[i]= players[i+1].getName();
      }
      ListView mListView = (ListView) findViewById(R.id.ListView_playersInfo);
      mListView.setAdapter(new ArrayAdapter<String>(this,
                           android.R.layout.simple_list_item_1, 
                           mItems));      
      if(doGameStart) {
         //: to do
         Intent mIntent= new Intent(this, MazeActivity.class);
         mIntent.putExtra("m", mapInfo.getM());
         mIntent.putExtra("n", mapInfo.getN());
         mIntent.putExtra("STEdges", mapInfo.getEdges());
         this.startActivity(mIntent);
      }
   }
   // -------------------------------------------------- 
   MyProgressDialog mProgressDialog;
   public void showMyDialog(String title, String msg) {
      if(mProgressDialog == null) {  // lazy new
         mProgressDialog= new MyProgressDialog(this);
      }
      
      if(!mProgressDialog.hasShowed()) {
         mProgressDialog.show(title,msg);
      } else {
         mProgressDialog.dismiss();
      }
   }   
   public void dismissMyDialog() {
      if(mProgressDialog != null) {
         if(mProgressDialog.hasShowed()) {
            mProgressDialog.dismiss();
         }
      }     
   }
   // --------------------------------------------------
   
}
